package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.OperatorElementReqTO;
import com.handydata.antifraud.to.dap.OperatorElementResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;


/**
 * 三要素查询
 *
 */
@Slf4j
@Service("operatorElementWsService")
@Comment("三要素验证")
public class OperatorElementWsServiceImpl extends DataPlatformWsService<OperatorElementReqTO, OperatorElementResTO>
	implements WsInvocationService<OperatorElementReqTO, OperatorElementResTO> {

	@Override
	public OperatorElementResTO query(OperatorElementReqTO req)	throws WsInvokeException {
		log.info("三要素验证请求参数 {}", req);
		OperatorElementResTO resTO = new OperatorElementResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("三要素验证参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("三要素验证接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("三要素验证参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("三要素验证接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("三要素验证参数校验 {}", "phone为空");
			resTO.setResCode("1111");
			resTO.setResMsg("三要素验证接口phone参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.OPERATOR_ELEMENT.getCode();
		resTO = executeQuery(req);
		// 行内压测挡板处理
//		try {
//			Thread.sleep(250);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		//模拟数据源返回成功结果  
		// success 1 3
//		String resultXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
//				"<subatm>"+
//				    "<application>GwBiz.Req</application>"+
//				    "<version>1.0.0</version>"+
//				    "<sendTime>20191016170059</sendTime>"+
//				    "<channelOrderId>20191016170059786</channelOrderId>"+
//				    "<transCode>100064</transCode>"+
//				    "<phone>15310154688</phone>"+
//				    "<name>陈敬</name>"+
//				    "<idCard>51022719690125308X</idCard>"+
//				    "<respCode>0000</respCode>"+
//				    "<respDesc>验证成功</respDesc>"+
//				"</subatm>";
		// fail 2
//		String resultXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
//				"<subatm>"+
//				    "<application>GwBiz.Req</application>"+
//				    "<version>1.0.0</version>"+
//				    "<sendTime>20191113115843</sendTime>"+
//				    "<channelOrderId>20191113115843089</channelOrderId>"+
//				    "<transCode>100064</transCode>"+
//				    "<phone>17375695026</phone>"+
//				    "<name>谭文敏</name>"+
//				    "<idCard>431127199710037818</idCard>"+
//				    "<respCode>1004</respCode>"+
//				    "<respDesc>信息不匹配</respDesc>"+
//				"</subatm>";
//		try {
//			Document doc = DocumentHelper.parseText(resultXml);
//			Element rootElt = doc.getRootElement(); // 获取根节点
//			String respCode = rootElt.elementTextTrim("respCode"); // 拿到subatm节点下的子节点respCode值
//			String respDesc = rootElt.elementTextTrim("respDesc"); // 拿到subatm节点下的子节点respDesc值
//			resTO.setRespCodeOfDataOperatorElement(respCode);
//			resTO.setResCode(respCode);
//			resTO.setResMsg(respDesc);
//		} catch (DocumentException e) {
//			e.printStackTrace();
//		}
//		// 挡板入库
//		DataCallLogsReqBO dataCallLogsReqBO = new DataCallLogsReqBO();
//		dataCallLogsReqBO.setId(UUID.randomUUID().toString().replace("-", ""));
//		dataCallLogsReqBO.setParams(JSONObject.toJSONString(req));
//		dataCallLogsReqBO.setElapsed(250);
//		dataCallLogsReqBO.setWselapsed(250);
//		dataCallLogsReqBO.setDataType(this.dataType);
//		dataCallLogsReqBO.setResCode("0000");
//		dataCallLogsReqBO.setResMsg("查询成功");
//		dataCallLogsReqBO.setResult(resultXml);
//		dataCallLogsReqBO.setOrderId(req.getOrderId());
//		dataCallLogsReqBO.setUpdateTime(new Date());
//		dataCallLogsReqBO.setCreateTime(new Date());
//		dataCallLogsService.createDataCallLogs(dataCallLogsReqBO);
		return resTO;
	}
	
	@Override
	protected String createQueryXml(OperatorElementReqTO req) {
		StringBuilder xml = new StringBuilder();        
        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        xml.append("<subatm>");
        xml.append("<application>GwBiz.Req</application>");
        xml.append("<version>1.0.0</version>");
        xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
        xml.append("<transCode>100064</transCode>");
        xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
        xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
        xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
        xml.append("<name>").append(req.getName()).append("</name>");
        xml.append("<idCard>").append(req.getIdCard()).append("</idCard>");
//        xml.append("<type>1</type>");
        xml.append("</subatm>");
        req.setQueryXml(xml.toString());
		return xml.toString();
	}
	
	@Override
	protected OperatorElementResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用三要素查询接口返回报文：{}", resultXml);
		OperatorElementResTO resTO = new OperatorElementResTO();
		try{
			// 解析xml，保存本地DB
			Document doc = DocumentHelper.parseText(resultXml);
	    	Element rootElt = doc.getRootElement(); // 获取根节点
//	    	String phone = rootElt.elementTextTrim("phone"); // 拿到subatm节点下的子节点phone值
//	        String name = rootElt.elementTextTrim("name"); // 拿到subatm节点下的子节点name值
//	        String idCard = rootElt.elementTextTrim("idCard"); // 拿到subatm节点下的子节点idCard值
	        String respCode = rootElt.elementTextTrim("respCode"); // 拿到subatm节点下的子节点respCode值
	        String respDesc = rootElt.elementTextTrim("respDesc"); // 拿到subatm节点下的子节点respDesc值
			resTO.setRespCodeOfDataOperatorElement(respCode);
			resTO.setResCode(respCode);
			resTO.setResMsg(respDesc);
			//resTO.setReqId(dataCallLogsReqBO.getId());
		} catch (Exception e){
			log.info("调用三要素查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用三要素查询接口返回报文解析异常");
		}
		log.info("调用三要素查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, OperatorElementReqTO.class);
	}
	
}

